package com.soup.faker.wrapper;

import com.soup.faker.api.Generator;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.IntSupplier;

/**
 * 列表生成器
 * 生成元素列表，返回 ArrayList
 * @param <T> 元素类型
 */
public class ListGenerator<T> implements Generator<List<T>> {
    private Generator<T> elementGenerator;
    private IntSupplier sizeSupplier;

    public ListGenerator(Generator<T> elementGenerator, IntSupplier sizeSupplier) {
        Objects.requireNonNull(elementGenerator);
        Objects.requireNonNull(sizeSupplier);
        this.elementGenerator = elementGenerator;
        this.sizeSupplier = sizeSupplier;
    }

    @Override
    public List<T> get() {
        int size = sizeSupplier.getAsInt();
        if(size <= 0) {
            return new ArrayList<>();
        }
        List<T> result = new ArrayList<>();
        while(size-- > 0) {
            result.add(elementGenerator.get());
        }
        return result;
    }
}
